<?php
class ControllerDelimanGiaobao extends Controller
{
	private $error = array();
   	function __construct() 
	{
		$listchek = $this->getListCheck();
		if(in_array($this->getMethod(),$listchek))
		{
			if(!$this->user->hasPermission($this->getRoute(), $this->getMethod()))
			{
				$this->response->redirect("?route=common/permission");
			}
		}
		
		
	 	$this->load->model("deliman/khachhang");
		$this->load->model("common/control");
		$this->load->model("deliman/khuvuc");
		$this->load->model("deliman/nhanvien");
		$this->load->model("deliman/kybao");
		
   	}
	
	public function index()
	{

		$this->getThongTinDatBaoList();

	}

	public function getkhachhang()
	{
		$nhanvienphutrachid = $this->user->getNhanVien();
		
		$this->load->model("common/control");
		$this->load->model('deliman/khuvuc');
		
		$khuvucid = $this->request->get['khuvucid'];
		$select =  $this->request->get['select'];
		$khuvus = array();
		$this->model_deliman_khuvuc->getTree($khuvucid, $khuvus);
		$arrkhuvucid = $this->string->matrixToArray($khuvus,'khuvucid');
		$where = " AND nhanvienphutrachid = '".$nhanvienphutrachid."' AND khuvucid in( '".implode("','",$arrkhuvucid)."')";
		$data_cbkhachhang = $this->model_deliman_khachhang->getList($where);
		
		
		$this->data['output'] = "<option value=''>----Chọn tất cả----</option>".$this->model_common_control->getDataCombobox($data_cbkhachhang, "hoten", "khachhangid", $select);
		
		$this->template='common/output.tpl';

		$this->render();
	}
	
	
	
	private function getThongTinDatBaoList() 
	{
		//lấy thông tin đặt báo có điều kiện		
		$data = $this->request->get;
		
		//lấy theo điều kiện khu vực id
		
		if($data['khuvucid'] !="")
		{
			$khuvucss = array();
			$this->model_deliman_khuvuc->getTree($data['khuvucid'], $khuvucss);
			$arrkhuvucid = $this->string->matrixToArray($khuvucss,'khuvucid');
			$wheres="AND nhanvienphutrachid = '".$this->user->getNhanVien()."' AND khuvucid in( '".implode("','",$arrkhuvucid)."')";
			
			$khachhang = $this->model_deliman_khachhang->getList($wheres);
			$khachhangid = $this->string->matrixToArray($khachhang,'khachhangid');
			$wh=" AND khachhangid in( '".implode("','",$khachhangid)."')";
			
			if (trim($data['tungay']) != "")
			{
				$wh.= " AND ngayphathanh >= '".$this->date->formatViewDate($data['tungay'])."'";
				//$rows = $this->model_deliman_khachhang->getThongTinDatBaoList($where);
			}
			if (trim($data['denngay']) != "")
			{
				$wh.= " AND ngayphathanh <= '".$this->date->formatViewDate($data['denngay'])."'";
				//$rows = $this->model_deliman_khachhang->getThongTinDatBaoList($where);
			}	
			if($data['khachhangid'] != "")
			{
				$wh.= " AND khachhangid = '".$data['khachhangid']."'";
				//$rows = $this->model_deliman_khachhang->getThongTinDatBaoList($where);
			}
			$wh.=" AND trangthai <> 'huy' order by ngayphathanh desc";;
			$rows = $this->model_deliman_khachhang->getThongTinDatBaoList($wh);
		}
		else
		{
			
			$nhanvienphutrachid = $this->user->getNhanVien();
			
			$this->load->model("common/control");
			$this->load->model('deliman/khuvuc');
			
			$khuvucid = $this->request->get['khuvucid'];
			$select =  $this->request->get['select'];
			$khuvus = array();
			$this->model_deliman_khuvuc->getTree($khuvucid, $khuvus);
			$arrkhuvucid = $this->string->matrixToArray($khuvus,'khuvucid');
			$data_cbkhachhang = $this->model_deliman_khachhang->getList(" AND nhanvienphutrachid = '".$nhanvienphutrachid."' AND khuvucid in( '".implode("','",$arrkhuvucid)."')");
			
			$arrkhachhangid = $this->string->matrixToArray($data_cbkhachhang,'khachhangid');
			$wh=" AND khachhangid in( '".implode("','",$arrkhachhangid)."')";
			if (trim($data['tungay']) != "")
			{
				$wh.= " AND ngayphathanh >= '".$this->date->formatViewDate($data['tungay'])."'";
			}
			if (trim($data['denngay']) != "")
			{
				$wh.= " AND ngayphathanh <= '".$this->date->formatViewDate($data['denngay'])."'";
			}	
			if($data['khachhangid'] != "")
			{
				$wh.= " AND khachhangid = '".$data['khachhangid']."'";
			}
			$wh.=" AND trangthai <> 'huy' order by ngayphathanh desc";;
			$rows = $this->model_deliman_khachhang->getThongTinDatBaoList($wh);
		}


		//ngay phat hanh max dua theo id loai bao
		$arrmaxdate = $this->model_deliman_kybao->getItemMaxDate();
		$this->data['maxdates'] = $arrmaxdate;
		
		
		//loc khu vuc theo user dang nhap
		$nhanvienphutrachid = $this->user->getNhanVien();
		$listkhuvucid = $this->model_deliman_nhanvien->getPhuTrachKhuVucList(" AND nhanvienphutrachid='".			$nhanvienphutrachid."' AND tinhtrang <> 'deleted'");
		$arrkhuvucid = $this->string->matrixToArray($listkhuvucid,'khuvucid');
		$this->data['dskhuvuc']=array();
		
		$dskhuvucid=$arrkhuvucid;
		
		foreach($dskhuvucid as $khuvucid)
		{
			$dskv=array();
			$this->model_deliman_khuvuc->getTree($khuvucid,$dskv);
			foreach($dskv as $kv)
			{
				array_push($this->data['dskhuvuc'], $kv);
			}
		}	
		$this->data['dskhuvuc'];
		
		
		
		
		$this->data['insert'] = $this->url->http('deliman/khachhang/insert');
		$this->data['delete'] = $this->url->http('deliman/khachhang/delete');		
		
		$this->data['datas'] = array();
		
		//Page
		$page = $this->request->get['page'];		
		$x=$page;		
		$limit = 20;
		$total = count($rows); 
		// work out the pager values 
		$this->data['pager']  = $this->pager->pageLayout($total, $limit, $page); 
		
		$pager  = $this->pager->getPagerData($total, $limit, $page); 
		$offset = $pager->offset; 
		$limit  = $pager->limit; 
		$page   = $pager->page;
		for($i=$offset;$i < $offset + $limit && count($rows[$i])>0;$i++)
		//for($i=0; $i <= count($this->data['datas'])-1 ; $i++)
		{
			$this->data['datas'][$i] = $rows[$i];
			$this->load->model("deliman/kybao");
			$kybao = $this->model_deliman_kybao->getItem($this->data['datas'][$i]['kybaoid']);
			$this->data['datas'][$i]['giaban'] = $kybao['giaban'] ;
			
			$khachhang = $this->model_deliman_khachhang->getItem($this->data['datas'][$i]['khachhangid']);
			$this->data['datas'][$i]['hoten'] = $khachhang['hoten'];
			$this->data['datas'][$i]['diachi'] = $khachhang['diachi'];
			
			
			$this->data['datas'][$i]['link_edit'] = $this->url->http('deliman/khachhang/update&phieuphanphoiid='.$this->data['datas'][$i]['phieuphanphoiid']);
			$this->data['datas'][$i]['text_edit'] = "Edit";
			
		}
		
		$this->data['nhanviengiao'] = $nhanvienphutrachid;
		
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		$this->id='content';
		$this->template="deliman/giaobao.tpl";
		$this->layout="layout/deliman";
		
		$this->render();
	}
	
	public function updatethongtindatbao()
	{
		$data = $this->request->post;
		$data['soluongdagiao'] = $this->string->toNumber($data['soluongdagiao']);
		if($this->validateGiaoBao($data))
		{
			$ngaygiao = $this->date->getToday();
			$this->model_deliman_khachhang->updateThongTinDatBao($data['id'],'ngaygiao',$ngaygiao);
			$this->model_deliman_khachhang->updateThongTinDatBao($data['id'],'comment',$data['ghichu']);
			
			//lay thong tin nhan vien
			$this->model_deliman_khachhang->updateThongTinDatBao($data['id'],'nhanviengiaoid',$data['nhanviengiaoid']);
			
		
			
			$rows = $this->model_deliman_nhanvien->getItem($data['nhanviengiaoid']);
			$this->model_deliman_khachhang->updateThongTinDatBao($data['id'],'masonhanviengiao',$rows['masonhanvien']);
			$this->model_deliman_khachhang->updateThongTinDatBao($data['id'],'tenhanviengiao',$rows['hoten']);
			
			
			$this->model_deliman_khachhang->updateThongTinDatBao($data['id'],'soluongdagiao',$data['soluongdagiao']);
			$this->model_deliman_khachhang->updateThongTinDatBao($data['id'],'trangthai','dagiao');
			$arr = array(
						'error'=>'',
						'id'=>$data['id'],
						'ngaygiao' => $this->date->formatMySQLDate($ngaygiao,'longdate'),
						'soluongdagiao' => $this->string->numberFormate($data['soluongdagiao'])
						);
			$this->data['output'] = json_encode($arr);
			
		}
		else
		{
			foreach($this->error as $item)
			{
				$this->data['output'] .= $item;
			}
			$arr = array(
					'error'=>$this->data['output']
					
					);
			$this->data['output'] = json_encode($arr);
		}
		
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function validateGiaoBao($data)
	{
		$arr_thongtindatbao =$this->model_deliman_khachhang->getThongTinDatBao($data['id']);
		if($arr_thongtindatbao['soluongdat'] < $data['soluongdagiao'])
		{
			$this->error['soluongdat'] = " Bạn đã nhập quá số lượng giao báo  ";
		}
		
		if($data['soluongdagiao'] == 0)
		{
			$this->error['soluongdagiao'] = " Bạn chưa nhập số lượng giao báo  ";
		}
		
		if (count($this->error)==0) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
	}
	//Cac ham xu ly tren form
	
}
?>